package br.folhapagamento.controle;

import java.io.IOException;
import java.sql.Date;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.folhapagamento.negocio.FolhaPagamentoNegocio;
import br.folhapagamento.negocio.FuncionarioNegocio;
import br.folhapagamento.to.FolhaPagamentoTO;
import br.folhapagamento.to.FuncionarioTO;

/**
 * Servlet implementation class FolhaPagamentoControle
 */
@WebServlet("/FolhaPagamentoControle")
public class FolhaPagamentoControle extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FolhaPagamentoControle() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Date dataEmissao = Date.valueOf(request.getParameter("dataEmissao"));
		int matricula = Integer.valueOf(request.getParameter("codMatricula"));
		
		FuncionarioNegocio funcNeg = new FuncionarioNegocio();
		FolhaPagamentoNegocio fpn = new FolhaPagamentoNegocio();
		String paginaResultado="";
		
		try{
			FuncionarioTO funcionario =  funcNeg.detalhar(matricula);
			FolhaPagamentoTO fpagto = fpn.confirmar(funcionario, dataEmissao);
			request.setAttribute("folha", fpagto);
			request.setAttribute("funcionario", funcionario);
			paginaResultado="folhaPagamentoConfirmar.jsp";
		} catch (SQLException e) {
			paginaResultado="erro.html";
			e.printStackTrace();
		} finally {
			RequestDispatcher view = request.getRequestDispatcher(paginaResultado);
			view.forward(request, response);
		}
		
	}

}
